/************************************************************************************/
/*  Program name CAPAPIMP_ACC_EVENT_ID.sql                                          */
/*  Creater:  WONG Wing Kin (PCCW - UniHub)                                         */
/*************   History   *************************                                */
/* 30 NOV, 2005   WONG Wing Kin         First created                               */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

ALTER TRIGGER AX_AP_INVOICE_DIST_ARDI1 DISABLE;
ALTER TRIGGER AX_AP_INVOICE_DIST_ARDI2 DISABLE;
ALTER TRIGGER AX_AP_INVOICE_DIST_ARU1 DISABLE;
ALTER TRIGGER AX_AP_INVOICE_DIST_ARU2 DISABLE;
ALTER TRIGGER AX_AP_INVOICE_DIST_BRU1 DISABLE;
--ALTER TRIGGER AX_AP_INVOICE_DIST_BRU2 DISABLE;
ALTER TRIGGER IGI_AP_INVOICE_DIST_T1 DISABLE;
ALTER TRIGGER IGI_AP_INVOICE_DIST_T2 DISABLE;
ALTER TRIGGER IGI_AP_INVOICE_DIST_T3 DISABLE;
ALTER TRIGGER JL_BR_AP_TAX_HOLDS DISABLE;

ALTER TRIGGER AP_SSE_NOTIFY_EMPLOYEE DISABLE;
ALTER TRIGGER AX_AP_INVOICE_PAY_ARDI1 DISABLE;
ALTER TRIGGER AX_AP_INVOICE_PAY_ARU1 DISABLE;
ALTER TRIGGER AX_AP_INVOICE_PAY_ARU2 DISABLE;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPAPIMP_ACC_EVENT_ID';

	lv_Scc_Count	NUMBER			:=	0;
	lv_Err_Count	NUMBER			:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

BEGIN

	BEGIN
		UPDATE	AP_INVOICE_DISTRIBUTIONS_ALL
		SET		ACCOUNTING_EVENT_ID	=	CAPBRIMP_MAPPING.GetAccEventIDMapping(ACCOUNTING_EVENT_ID)
		WHERE	INVOICE_DISTRIBUTION_ID	IN
			(
				SELECT
					INVOICE_DISTRIBUTION_ID_N
				FROM
					CRST_AP_INVOICES_DIST_MAP
			);

		UPDATE	AP_INVOICE_PAYMENTS_ALL
		SET		ACCOUNTING_EVENT_ID	=	CAPBRIMP_MAPPING.GetAccEventIDMapping(ACCOUNTING_EVENT_ID)
		WHERE	INVOICE_PAYMENT_ID	IN
			(
				SELECT
					INVOICE_PAYMENT_ID_N
				FROM
					CRST_AP_INV_PAYMENT_MAP
			);

		UPDATE	AP_PAYMENT_HISTORY_ALL
		SET		ACCOUNTING_EVENT_ID	=	CAPBRIMP_MAPPING.GetAccEventIDMapping(ACCOUNTING_EVENT_ID)
		WHERE	PAYMENT_HISTORY_ID	IN
			(
				SELECT
					PAYMENT_HISTORY_ID_N
				FROM
					CRST_AP_PAY_HISTORY_MAP
			);

		lv_Scc_Count	:=	lv_Scc_Count + 1;
	EXCEPTION
		WHEN OTHERS THEN
			DBMS_OUTPUT.Put_Line(SQLERRM);
			lv_Err_Count	:=	lv_Err_Count + 1;
	END;

	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/

ALTER TRIGGER AX_AP_INVOICE_DIST_ARDI1 ENABLE;
ALTER TRIGGER AX_AP_INVOICE_DIST_ARDI2 ENABLE;
ALTER TRIGGER AX_AP_INVOICE_DIST_ARU1 ENABLE;
ALTER TRIGGER AX_AP_INVOICE_DIST_ARU2 ENABLE;
ALTER TRIGGER AX_AP_INVOICE_DIST_BRU1 ENABLE;
--ALTER TRIGGER AX_AP_INVOICE_DIST_BRU2 ENABLE;
ALTER TRIGGER IGI_AP_INVOICE_DIST_T1 ENABLE;
ALTER TRIGGER IGI_AP_INVOICE_DIST_T2 ENABLE;
ALTER TRIGGER IGI_AP_INVOICE_DIST_T3 ENABLE;
ALTER TRIGGER JL_BR_AP_TAX_HOLDS ENABLE;

ALTER TRIGGER AP_SSE_NOTIFY_EMPLOYEE ENABLE;
ALTER TRIGGER AX_AP_INVOICE_PAY_ARDI1 ENABLE;
ALTER TRIGGER AX_AP_INVOICE_PAY_ARU1 ENABLE;
ALTER TRIGGER AX_AP_INVOICE_PAY_ARU2 ENABLE;
